package marking.liu.listener;


import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import marking.liu.controller.StockController;
import marking.liu.service.StockService;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MqListener {

    @Autowired
    private Cache<String,Object> caffeineCache;

    @Autowired
    private StockService stockService;

    @Autowired
    private StockController stockController;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "innerMarketQueue"),
            exchange = @Exchange(value = "innerMarketTopicExchange"),
            key = {"inner.market"}

    )})
    public void acceptInnerMarketInfo(Date date)throws Exception{
        log.info("开始启动消费者");
        //获取时间毫秒差值
        long diffTime= DateTime.now().getMillis()-new DateTime(date).getMillis();
        //超过一分钟告警
        if (diffTime>60000) {
            log.error("采集国内大盘时间点：{},同步超时：{}ms");
        }
        //将缓存置为失效删除
        caffeineCache.invalidate("innerMarketInfosKey");
        //调用服务更新缓存
//        TODO
        stockController.getInnerMarketInfo();
    }

}
